package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.AdminOrdersMapper;
import cn.scxy.bookcity.pojo.dto.AdminOrderLikeDTO;
import cn.scxy.bookcity.pojo.entity.Orders;
import cn.scxy.bookcity.pojo.vo.AdminOrdersByIdVO;
import cn.scxy.bookcity.pojo.vo.AdminOrdersGoodsListVO;
import cn.scxy.bookcity.pojo.vo.AdminOrdersListVO;
import cn.scxy.bookcity.pojo.vo.GoodsListItemVO;
import cn.scxy.bookcity.service.IAdminOrderService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AdminOrdersImpl implements IAdminOrderService {

    @Autowired
    private AdminOrdersMapper mapper;
    @Override
    public List<AdminOrdersListVO> selectOrderList() {
        List<AdminOrdersListVO> list=mapper.selectOrderList();
        return list;
    }

    @Override
    public List<AdminOrdersGoodsListVO> selectGoodsList(Long id) {
        List<AdminOrdersGoodsListVO> list=mapper.selectGoodsList(id);
        return list;
    }

    @Override
    public List<AdminOrdersListVO> notPayList() {
        List<AdminOrdersListVO> list=mapper.notPayList();
        return list;
    }
    @Override
    public List<AdminOrdersListVO> PayList() {
        List<AdminOrdersListVO> list=mapper.PayList();
        return list;
    }
    @Override
    public List<AdminOrdersListVO> deliverList() {
        List<AdminOrdersListVO> list=mapper.deliverList();
        return list;
    }
    @Override
    public List<AdminOrdersListVO> receiptList() {
        List<AdminOrdersListVO> list=mapper.receiptList();
        return list;

    }

    @Override
    public int selectOrderCount() {
       int i= mapper.selectOrderCount();
        return i;
    }

    @Override
    public List<AdminOrdersListVO> selectLikeOrders(AdminOrderLikeDTO adminOrderLikeDTO) {
        List<AdminOrdersListVO> list=mapper.selectLikeOrders(adminOrderLikeDTO);
        return list;
    }


    @Override
    public void setPay(Long id) {
        updateStateById(id, 1);
    }
    @Override
    public void setNotpay(Long id) {
        updateStateById(id, 0);
    }
    @Override
    public void setDeliver(Long id) {
        updateStateById(id, 2);
    }
   @Override
    public void setReceipt(Long id) {
        updateStateById(id, 3);
    }

    private void updateStateById(Long id, Integer state) {
        String[] tips = {"未支付", "支付未发货","已支付已发货","发货未收货"};
        log.debug("开始处理【{}订单】的业务，参数：{}", tips[state], id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        AdminOrdersByIdVO order = mapper.getOrderById(id);
        // 判断查询结果是否为null
        if (order == null) {
            String message = tips[state] + "订单失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable是否为1
        if (order.getState().equals(state)&&order.getState()>state) {
            String message = tips[state] + "订单失败，当前订单已经处于" + tips[state] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 准备执行更新
        AdminOrdersByIdVO order1 = new AdminOrdersByIdVO();
        order1.setId(id);
        order1.setState(state);
        switch (state){
            case 1 : order1.setPaymentTime(new Date());break;
            case 2 : order1.setSendTime(new Date()); break;
            case 3 : order1.setFurlTime(new Date()); break;
        }
        int rows = mapper.update(order1);
        if (rows != 1) {
            String message = tips[state] + "失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }
}
